package com.yvdedu.hospital.controller;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yvdedu.hospital.entity.Hospital;
import com.yvdedu.hospital.service.HospitalService;


@Controller
@RequestMapping("hospital")
public class HospitalController {
	
	@Resource
	HospitalService hospitalService;
	
	List<Hospital> list;

	
//	添加
	@RequestMapping("add")
	public void add(){
	}

//	执行添加
	@RequestMapping("doadd")
	public Object doadd( Hospital hospital, Model model){
		
//		System.out.println(hospital);
		list = hospitalService.hospitalNamequery(hospital);
		if ( list.size()>0 ){
			model.addAttribute("data","分院名已存在！");
			model.addAttribute("acc", "false");
			model.addAttribute("url", "add");
			return "hospital/tips";
		}else {
			
			//得到long类型当前时间
			long ltime = System.currentTimeMillis();
			//new日期对象
			Date date = new Date(ltime);
			//转换提日期输出格式
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
			String time = dateFormat.format(date);
			hospital.setCreateDate(time);
			hospitalService.hospitaladd(hospital);
			model.addAttribute("acc", "true");
			model.addAttribute("url", "list");
			return "hospital/tips";
//			return "redirect:list";
		}
	}
	

//	列表
	@RequestMapping("list")
	public void list( Model model ){
		
		list = hospitalService.hospitalquery();
//		System.out.println(list);
		model.addAttribute("hospitalList",list);
	}
	
//	查询
	@RequestMapping("search")
	public Object search( Hospital hospital,String hospitalName,Model model ){
		System.out.println(hospitalName);
		
		hospital.setHospitalName(hospitalName);
	
		list = hospitalService.hospitalNamequeryl(hospital);
		
		System.out.println("******************************************"+list);
		model.addAttribute("hospitalList",list);
		return "hospital/list";
	}

	
//	编辑
	@RequestMapping("edit")
	public void edit(){
	}
	
//	执行编辑
	@RequestMapping("doedit")
	public Object doedit(Hospital hospital,Model model ){
//		System.out.println(hospital+"\n");
		hospitalService.hospitaledit(hospital);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "list");
		return "hospital/tips";
//		return "redirect:list";
	}
	
//	删除
	@RequestMapping("delete")
	public Object delete( int id,Model model ){
		hospitalService.hospitaldelete(id);
		model.addAttribute("acc", "true");
		model.addAttribute("url", "list");
		return "hospital/tips";
//		return "redirect:list";
	}
	
	
	
}
